Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
These other methods do not return the downloaded data in the same way that OpenURL does. Rather, the data is placed in the Internet Transfer control’s internal buffer. Before getting to the details of the methods, we need to see how you retrieve data from this buffer. First, let’s look at the Internet Transfer control’s single event, which is essential for retrieving data from the buffer. What State Am I In? The Internet Transfer control’s one event occurs whenever the state of the control changes, reflecting a change in the connection status of the control, the receipt of some data, or some other communication event that your program may need to know about (including, as we will see, errors). The associated event procedure has the following declaration:
    Inet1_StateChanged(ByVal State As Integer)
State is a value identifying the new state of the control. The possible values, the defined constants, and their meanings are described in Table 18.1. Using the StateChanged event is an essential part of using the Internet Transfer control’s advanced methods—that is, methods other than OpenURL. We’ll see how soon. You can also use this event to provide status messages to the user, giving information on what tasks the control is doing or has completed. Listing 18.1 shows the code to do this. In this example, I am using a Text Box control named txtStatus to display the status messages. You could just as well use various other methods, such as a status bar, to display the messages. You should note that the control state identified by the number 11, icError, indicates that an error has occurred. But what error? Lots can go wrong when trying to transfer information over the Internet. Fortunately, the Internet Transfer control has two properties, ResponseCode and ResponseInfo, that provide a numerical code and a text description
Table 18.1 State values for the StateChanged event.
Constant | Value | Description
--- | --- | ---
icNone | 0 | No state to report.
icResolvingHost | 1 | The control is looking up the IP address of the specified host computer.
icHostResolved | 2 | The control successfully found the IP address of the specified host computer.
icConnecting | 3 | The control is connecting to the host computer.
icConnected | 4 | The control successfully connected to the host computer.
icRequesting | 5 | The control is sending a request to the host computer.
icRequestSent | 6 | The control successfully sent the request.
icReceivingResponse | 7 | The control is receiving a response from the host computer.
icResponseReceived | 8 | The control successfully received a response from the host computer.
icDisconnecting | 9 | The control is disconnecting from the host computer.
icDisconnected | 10 | The control successfully disconnected from the host computer.
icError | 11 | An error occurred in communicating with the host computer.
icResponseCompleted | 12 | The request has completed, and all data has been received.
of the most recent error. You can see that the code in Listing 18.1 uses these properties to provide the user with a description of any error that occurs. Listing 18.1 Using the StateChanged event to provide status messages to the user.
    Private Sub Inet1_StateChanged(ByVal State As Integer)

    Select Case State
        Case icResolvingHost ‘ 1
            txtStatus.Text = “Looking up IP address of host computer”
        Case icHostResolved  ‘ 2
            txtStatus.Text = “IP address found”
        Case icConnecting ‘ 3
            txtStatus.Text = “Connecting to host computer”
        Case icConnected ‘ 4
            txtStatus.Text = “Connected”
        Case icRequesting ‘ 5
            txtStatus.Text = “Sending a request to host computer”
        Case icRequestSent ‘ 6
            txtStatus.Text = “Request sent”
        Case icReceivingResponse ‘ 7
            txtStatus.Text = “Receiving a response from host computer”
        Case icResponseReceived ‘ 8
            txtStatus.Text = “Response received”
        Case icDisconnecting ‘ 9
            txtStatus.Text = “Disconnecting from host computer”
        Case icDisconnected ‘ 10
            txtStatus.Text = “Disconnected”
        Case icError ‘ 11
            txtStatus.Text = “Error ” & Inet1.ResponseCode & _
              “ ” & Inet1.ResponseInfo
        Case icResponseCompleted ‘ 12
            txtStatus.Text = “Request completed - all data received”
    End Select

    End Sub
You should be aware that StateChanged events are generated by the OpenURL method, even though, given the fact that this method operates synchronously, the events are not really needed. Problems can arise in a program that uses OpenURL, as well as the other asynchronous methods, because the StateChanged event will be fired, and the associated code executed, at times when it is not needed and may, in fact, cause mischief. The solution I use for this potential problem is to maintain a global flag that indicates when OpenURL is active:
    Dim UsingOpenURL As Boolean
    ...

    UsingOpenURL = True
    buf = Inet1.OpenURL(...)
    UsingOpenURL = False
    Then in the StateChanged event procedure:

    Private Sub Inet1_StateChanged(ByVal State As Integer)

    If UsingOpenURL Then Exit Sub
    ...

    End Sub
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.